package edu.task.service.impl;

import edu.task.model.SysMenu;
import edu.task.model.vo.MetaVo;
import edu.task.model.vo.RouterVO;
import edu.task.model.vo.SysMenuVO;
import edu.task.model.vo.SysUserVO;
import edu.task.repository.conscientiousSysMenuRepository;
import edu.task.service.SysMenuService;
import edu.task.utils.BeanUtils;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    conscientiousSysMenuRepository conscientiousSysMenuRepository;

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        return conscientiousSysMenuRepository.selectMenuListByRoleId(roleId);
    }

    @Override
    public Set<String> selectMenuPermsByUserId(Long userId) {
        List<String> perms= conscientiousSysMenuRepository.selectMenuPermsByUserId(userId);
        Set<String> permsSet=new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysMenuVO> selectMenuTreeByUserId(Long userId) {
        List<SysMenu> menus=null;
        if (SysUserVO.isAdmin(userId)) {
            menus= conscientiousSysMenuRepository.selectMenuTreeAll();
        }else {
            menus= conscientiousSysMenuRepository.selectMenuTreeByUserId(userId);
        }
        List<SysMenuVO> menuVOS= BeanUtils.copyProperties(menus,SysMenuVO.class);
        return getChildPerms(menuVOS,0);
    }

    private List<SysMenuVO> getChildPerms(List<SysMenuVO> menuVOS, int i) {
        List<SysMenuVO> returnList=new ArrayList<SysMenuVO>();
        for (Iterator<SysMenuVO> iterator=menuVOS.iterator();iterator.hasNext();){
            SysMenuVO t= iterator.next();
            if (t.getParentId()==i) {
                recursionFn(menuVOS,t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    private void recursionFn(List<SysMenuVO> list, SysMenuVO t) {
        List<SysMenuVO> childList=getChildList(list,t);
        t.setChildren(childList);
        for (SysMenuVO sysMenuVO : childList) {
            if (hassChild(list,sysMenuVO)){
                Iterator<SysMenuVO> it=childList.iterator();
                while (it.hasNext()){
                    SysMenuVO n=it.next();
                    recursionFn(list,n);
                }
            }
        }
    }

    private boolean hassChild(List<SysMenuVO> list, SysMenuVO sysMenuVO) {
        return getChildList(list,sysMenuVO).size()>0?true:false;
    }

    private List<SysMenuVO> getChildList(List<SysMenuVO> list, SysMenuVO t) {
        List<SysMenuVO> tlist=new ArrayList<SysMenuVO>();
        Iterator<SysMenuVO> it=list.iterator();
        while (it.hasNext()) {
            SysMenuVO n=it.next();
            if (n.getParentId().longValue()==t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return list;
    }

    @Override
    public List<RouterVO> buildMenus(List<SysMenuVO> menus) {
        List<RouterVO> routers=new LinkedList<RouterVO>();
        for (SysMenuVO menu : menus) {
            RouterVO router=new RouterVO();
            router.setName(StringUtils.capitalize(menu.getPath()));
            router.setPath(getRouterPath(menu));
            router.setComponent(StringUtils.isEmpty(menu.getComponent())?"Layout":menu.getComponent());
            router.setMeta(new MetaVo(menu.getMenuName(),menu.getIcon()));
            List<SysMenuVO> cMenus=menu.getChildren();
            if (!cMenus.isEmpty()&&cMenus.size()>0&&"M".equals(menu.getMenuType())) {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildMenus(cMenus));
            }
            routers.add(router);
        }
        return routers;
    }

    private String getRouterPath(SysMenuVO menu) {
        String routerPath=menu.getPath();
        if (0==menu.getParentId()&&"1".equals(menu.getIsFrame())) {
            routerPath="/"+menu.getPath();
        }
        return routerPath;
    }
}
